import _regeneratorRuntime from "@babel/runtime/regenerator";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _map from "lodash/map";

/**
 * docType.js - 单据权限 model
 * @date: 2018-10-29
 */
import uuidv4 from 'uuid/v4';
import { getResponse, createPagination } from 'utils/utils';
import { queryMapIdpValue } from 'cpaas-front/lib/services/api';
import { queryDocType as _queryDocType, queryDocTypeDetail as _queryDocTypeDetail, addDocType, updateDocType, queryDocTypeAuth, updateDocTypeAuth as _updateDocTypeAuth } from "../services/docTypeService";
export default {
  namespace: 'docType',
  state: {
    docTypeList: {},
    // 单据类型定义列表
    docTypeDetail: {},
    // 单据类型定义详情
    docTypeAuth: [],
    // 单据类型权限维度
    docTypeLevelCode: [],
    // 单据权限层级编码
    // typeList: [],
    pagination: {},
    // 单据权限列表分页对象
    personalList: [],
    businessList: [],
    ruleTypeList: [],
    docTypeSqlidList: []
  },
  effects: {
    queryLevelCode: /*#__PURE__*/_regeneratorRuntime.mark(function queryLevelCode(_, _ref) {
      var call, put, res, response;
      return _regeneratorRuntime.wrap(function queryLevelCode$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.next = 3;
              return call(queryMapIdpValue, {
                docTypeLevelCode: 'CIAM.DOC_TYPE_LEVEL_CODE',
                typeList: 'CIAM.DOC.AUTH_TYPE',
                ruleTypeList: 'CIAM.DOC.RULE_TYPE'
              });

            case 3:
              res = _context.sent;
              response = getResponse(res);
              _context.next = 7;
              return put({
                type: 'updateState',
                payload: _objectSpread({}, response)
              });

            case 7:
            case "end":
              return _context.stop();
          }
        }
      }, queryLevelCode);
    }),
    // 查询单据类型定义列表
    queryDocType: /*#__PURE__*/_regeneratorRuntime.mark(function queryDocType(_ref2, _ref3) {
      var payload, call, put, res, docTypeList, pagination;
      return _regeneratorRuntime.wrap(function queryDocType$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              call = _ref3.call, put = _ref3.put;
              _context2.next = 4;
              return call(_queryDocType, payload);

            case 4:
              res = _context2.sent;
              docTypeList = getResponse(res);
              pagination = createPagination(docTypeList);
              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  docTypeList: docTypeList,
                  pagination: pagination
                }
              });

            case 9:
            case "end":
              return _context2.stop();
          }
        }
      }, queryDocType);
    }),
    // 查询单据类型定义详情
    queryDocTypeDetail: /*#__PURE__*/_regeneratorRuntime.mark(function queryDocTypeDetail(_ref4, _ref5) {
      var payload, call, put, res, docTypeDetail, _docTypeDetail$docTyp, docTypeSqlidList;

      return _regeneratorRuntime.wrap(function queryDocTypeDetail$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              call = _ref5.call, put = _ref5.put;
              _context3.next = 4;
              return call(_queryDocTypeDetail, payload);

            case 4:
              res = _context3.sent;
              docTypeDetail = getResponse(res);
              _docTypeDetail$docTyp = docTypeDetail.docTypeSqlidList, docTypeSqlidList = _docTypeDetail$docTyp === void 0 ? [] : _docTypeDetail$docTyp;
              _context3.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  docTypeDetail: docTypeDetail,
                  docTypeSqlidList: _map(docTypeSqlidList, function (r) {
                    return _objectSpread(_objectSpread({}, r), {}, {
                      tableId: uuidv4()
                    });
                  })
                }
              });

            case 9:
            case "end":
              return _context3.stop();
          }
        }
      }, queryDocTypeDetail);
    }),
    // 新增或更新单据类型定义
    saveDocType: /*#__PURE__*/_regeneratorRuntime.mark(function saveDocType(_ref6, _ref7) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function saveDocType$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call;

              if (!payload.docTypeId) {
                _context4.next = 8;
                break;
              }

              _context4.next = 5;
              return call(updateDocType, payload);

            case 5:
              res = _context4.sent;
              _context4.next = 11;
              break;

            case 8:
              _context4.next = 10;
              return call(addDocType, payload);

            case 10:
              res = _context4.sent;

            case 11:
              return _context4.abrupt("return", getResponse(res));

            case 12:
            case "end":
              return _context4.stop();
          }
        }
      }, saveDocType);
    }),
    // 查询单据类型权限维度
    queyDocTypeAuth: /*#__PURE__*/_regeneratorRuntime.mark(function queyDocTypeAuth(_ref8, _ref9) {
      var payload, call, put, docTypeAuth, result;
      return _regeneratorRuntime.wrap(function queyDocTypeAuth$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              call = _ref9.call, put = _ref9.put;
              _context5.next = 4;
              return call(queryDocTypeAuth, payload);

            case 4:
              docTypeAuth = _context5.sent;
              result = getResponse(docTypeAuth);
              _context5.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  docTypeAuth: result
                }
              });

            case 8:
              return _context5.abrupt("return", result);

            case 9:
            case "end":
              return _context5.stop();
          }
        }
      }, queyDocTypeAuth);
    }),
    // 更新单据类型权限维度
    updateDocTypeAuth: /*#__PURE__*/_regeneratorRuntime.mark(function updateDocTypeAuth(_ref10, _ref11) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateDocTypeAuth$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref10.payload;
              call = _ref11.call;
              _context6.next = 4;
              return call(_updateDocTypeAuth, payload);

            case 4:
              res = _context6.sent;
              return _context6.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context6.stop();
          }
        }
      }, updateDocTypeAuth);
    })
  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};